//
//  GTNewfeatureCell.m
//  GiftTalk
//
//  Created by Wan on 16/4/19.
//  Copyright © 2016年 Wan. All rights reserved.
//

#import "GTNewfeatureCell.h"
#import <View+MASAdditions.h>

#import "GTTabBarController.h"

@interface GTNewfeatureCell ()

/** 开始按钮 */
@property (strong, nonatomic) UIButton *startButton;
/** 引导图片 */
@property (strong, nonatomic) UIImageView *imageView;
@end

@implementation GTNewfeatureCell

#pragma mark - 懒加载

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}

- (UIButton *)startButton {
    if (!_startButton) {
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:@"开启礼物说" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(startBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        btn.hidden = YES;
        _startButton = btn;
    }
    return _startButton;
}

#pragma mark Lift Cycle

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 初始化 UI
        [self setupUI];
    }
    return self;
}

#pragma makr - Create UI

- (void)setupUI {
    // 1. 添加子控件到 contentView 上
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.startButton];
    
    // 2. 布局子控件的位置
    self.imageView.frame = CGRectMake(0, 0, GTScreenW, GTScreenH);
    self.startButton.frame = CGRectMake(GTScreenW * 0.35, GTScreenH * 0.85, GTScreenW * 0.3, 44);
    
}

- (void)setImage:(UIImage *)image {
    _image = image;
    
    self.imageView.image = image;
}

#pragma mark - Other Function

- (void)setIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count {
    // 最后一个,显示开始按钮
    if (indexPath.row == count - 1) {
        self.startButton.hidden = NO;
        self.startButton.layer.cornerRadius = 10;
        self.startButton.layer.masksToBounds = YES;
        self.startButton.layer.backgroundColor = [UIColor whiteColor].CGColor;
        self.startButton.layer.borderColor = [UIColor redColor].CGColor;
        self.startButton.layer.borderWidth = 2.0;
    } else {
        self.startButton.hidden = YES;
    }
}

/**
 让 cell 按钮执行动画
 */
- (void)startBtnAnimation {
    self.startButton.hidden = false;
    
    // 执行动画
    self.startButton.transform = CGAffineTransformMakeScale(0.0, 0.0);
    self.startButton.userInteractionEnabled = false;
    
    // 带弹跳效果的动画，IOS7 退出，IOS 正式使用
    // Swift 中的 UIViewAnimationOptions(rawValue: 0) == OC 中的 knilOption
    [UIView animateWithDuration:2.0 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:10 options:(UIViewAnimationOptionLayoutSubviews) animations:^{
        // 清空形变
        self.startButton.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        // 恢复按钮交互事件
        self.startButton.userInteractionEnabled = true;
    }];
}

- (void)startBtnClick {
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[GTTabBarController alloc]];
//    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"GTSwitchRootViewControllerKey" object:@true];
}

@end
